home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / MacGofer 0.22d / MacGofer 0.22d Release / Gofer Documentation / Gofer Manual / ch06 < prev    next >
Text File  |  1991-12-30  |  10KB  |  90 lines

  1. form "x `op` y" is equivalent  to
  2.      the corresponding expression "op x y", whilst an  expression  such
  3.      as "f x y z" can also be written as "(x `f` y) z".
  4.  
  5.      [NOTE: For those using Gofer on a  PC,  you  may  find  that  your
  6.      keyboard does not have a backquote key!  In this case  you  should
  7.      still be able to enter a backquote by holding down the key  marked
  8.      ALT, pressing the keys '9' and then '6' on the numeric keypad  and
  9.      then releasing the ALT key.]
  10.  
  11.   o  Any  operator symbol  can be treated as an identifier by enclosing
  12.      it in parentheses.  For example, the addition function denoted  by
  13.      the operator symbol "+" is often written as "(+)".  Any expression
  14.      of the form "x + y" can also be written in the form "(+) x y".
  15.  
  16. There are two more technical problems which have to be dealt with  when
  17. working with operator symbols:
  18.  
  19.   o  Precedence: Given operator symbols (+) and (*), should "2 * 3 + 4"
  20.      be treated as either "(2 * 3) + 4" or "2 * (3 + 4)"?
  21.  
  22.      This problem is solved by assigning  each  operator  a  precedence
  23.      value (an integer in the range 0 to 9).  In a  situation  such  as
  24.      the  above,  we  simply  compare  the  precedence  values  of  the
  25.      operators involved, and carrying out  the  calculation  associated
  26.      with  the  highest  precedence  operator  first.    The   standard
  27.      precedence values for (+) and (*) are 6 and 7 respectively so that
  28.      the expression above will actually be treated as "(2 * 3) + 4".
  29.  
  30.   o  Grouping: The above rule  is only useful when the operator symbols
  31.  
  32.  
  33.                                       9
  34.  
  35.  
  36.  
  37.  
  38. Introduction to Gofer         6. FUNCTION NAMES - IDENTIFIERS AND OPERATORS     
  39.  
  40.  
  41.      involved have  distinct  precedences.   For  example,  should  the
  42.      expression "1 - 2 - 3" be treated as either "(1 - 2) - 3" giving a
  43.      result of -4, or as "1 - (2 - 3)" giving a result of 2?
  44.  
  45.      This problem is  solved  by  giving  each  operator  a  `grouping'
  46.      (sometimes called its associativity).  An operator symbol  (-)  is
  47.      said to:
  48.  
  49.        o  group to the left  if "x - y - z" is treated as "(x - y) - z"
  50.  
  51.        o  group to the right if "x - y - z" is treated as "x - (y - z)"
  52.  
  53.      A third possibility is that an expression of the form "x - y -  z"
  54.      is to be treated as ambiguous and will  be  flagged  as  a  syntax
  55.      error.   In  this  case  we  say  that   the   operator   (-)   is
  56.      non-associative.
  57.  
  58.      The standard approach in Gofer is to treat (-) as grouping to  the
  59.      left so that "1 - 2 - 3" will actually be treated as "(1-2)-3".
  60.  
  61. By  default,  every  operator   symbol   in   Gofer   is   treated   as
  62. non-associative with precedence 9.  These values can be  changed  by  a
  63. declaration of one of the following forms:
  64.  
  65.     infixl digit ops      to declare operators which group to the left
  66.     infixr digit ops      to declare operators which group to the right
  67.     infix  digit ops      to declare non-associative operators
  68.  
  69. In each of these declarations ops represents a  list  of  one  or  more
  70. operator symbols separated by commas and digit is an integer between  0
  71. and 9 which gives the precedence value for each of the listed  operator
  72. symbols.  The precedence digit may be omitted in which case a value  of
  73. 9 is assumed.  There are a number of restrictions on the use  of  these
  74. declarations:
  75.  
  76.   o  Operator  declarations  can  only  appear  in  files  of  function
  77.      definitions which are loaded into Gofer; they  cannot  be  entered
  78.      directly whilst using the Gofer interpreter.
  79.  
  80.   o  At most one operator declaration is permitted for  any  particular
  81.      operator symbol (even if repeated  declarations  all  specify  the
  82.      same precedence and grouping as the original declaration).
  83.  
  84.   o  Any file containing a declaration for an operator  precedence  and
  85.      grouping must also contain  a  (top-level)  declaration  for  that
  86.      operator.
  87.  
  88. In theory, it is possible to use an operator declaration at  any  point
  89. in a file of definitions.  In practice, it is sensible to  ensure  that
  90. each operator is